package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.wms.wms.constant.FilePathConstant;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.digester.xmlrules.DigesterLoadingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.OutputStream;

@RestController
@Api(tags = "文件流")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class FileController {




    @ApiOperation(value = "获取文件流",notes = "获取文件流")
    @RequestMapping(value = "/getFile/{fileName}", method = RequestMethod.GET)
    public void getFile(HttpServletResponse response,@PathVariable("fileName") String fileName) {

        //String file = "D:\\发货单.xlsx";
        try {
            FileInputStream inputStream = new FileInputStream(FilePathConstant.BASE_PATH+ fileName);
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);

            response.setContentType("application/xlsx");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" +fileName);
            OutputStream os = response.getOutputStream();

            os.write(data);
            //先声明的流后关掉！
            os.flush();
            os.close();
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();

        }
    }




}
